﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.ViewModel;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Events;
using System.Collections.Generic;
using Microsoft.Practices.Prism.Commands;
using System.ServiceModel.DomainServices.Client;
using UI.Web.SL.Demo.Infrastructure.Events;
using SailHero.Soft.FrameWork.Models;
using UI.Web.SL.Demo.RIAServices.Web;

namespace UI.Web.SL.Demo.Modules.Customer
{
    [Export(typeof(CustomerListViewModel))]
    public class CustomerListViewModel : NotificationObject
    {
        private IEventAggregator _eventAggregator;
        private IEnumerable<CUSTOMER> _customerList;
        private ICommand _deleteCustomerCommand;
        private CustomerDContext _customerDContext = new CustomerDContext();
        private LoadOperation<CUSTOMER> _loadCustomerListOperation; 

        [ImportingConstructor]
        public CustomerListViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            _eventAggregator.GetEvent<AddCustomerCompletedEvent>().Subscribe(LoadCustomer);
            LoadCustomer(null);
        }

        public IEnumerable<CUSTOMER> CustomerList
        {
            get { return _customerList; }
            set
            {
                _customerList = value;
                base.RaisePropertyChanged("CustomerList");
            }
        }

        public ICommand DeleteCustomerCommand
        {
            get
            {
                if (_deleteCustomerCommand == null)
                    _deleteCustomerCommand = new DelegateCommand<CUSTOMER>(DeleteCustomer);

                return _deleteCustomerCommand;
            }
        }

        public void LoadCustomer(object o)
        {
            _loadCustomerListOperation = _customerDContext.Load(_customerDContext.GetCustomersQuery());
            _loadCustomerListOperation.Completed += new EventHandler(_loadCustomerListOperation_Completed);
        }

        void _loadCustomerListOperation_Completed(object sender, EventArgs e)
        {
            this.CustomerList = _loadCustomerListOperation.Entities;
        }

        private void DeleteCustomer(CUSTOMER customer)
        {
            _customerDContext.CUSTOMERs.Remove(customer);
            _customerDContext.SubmitChanges(DeleteCustomerCompleted, customer);
        }

        private void DeleteCustomerCompleted(SubmitOperation operation)
        {
            if (operation.IsComplete)
            {
                LoadCustomer(null);

                var customer = (CUSTOMER)operation.UserState;
                _eventAggregator.GetEvent<LoggingEvent>().Publish(
                    string.Format("删除Customer：Name={0},Age={1}", customer.NAME, customer.AGE));
            }
        }
    }
}
